<?php

/*
 * Controller/TransfertController.php
 */

namespace RHX\Controller;

class TransfertController extends RHXController {

    public function indexAction() {
	// Tables
	$siteTable = $this->getService('Table/SiteTable');

	// Variables
	$siteId	 = $this->params()
		->fromRoute('id', -1);
	$type	 = $this->params()
		->fromRoute('other1', 1);
	$refresh = $this->params()
		->fromRoute('other2', 2);

	// Site en cours
	$site = null;
	if ($siteId < 0) {
	    $site	 = $this->navigation->offsetGet('site');
	    $refresh = 1;
	}
	else {
	    $site = $siteTable->get($siteId);
	    $this->changeSite($siteId);
	}

	return array(
	    'site'		 => $site,
	    'type'		 => $type,
	    'refresh'	 => ((int) $refresh == 1) ? true : false,
	);
    }

    public function loadIndexAction() {
	// changement de layout
	$this->layout('layout/ajax');

	// table
	$transfertTable = $this->getService('Table\TransfertTable');

	// Variables
	$siteId	 = $this->params()->fromRoute('id', 1);
	$typeId	 = $this->params()->fromRoute('other1', 1);
	$this->changeSite($siteId);

	// Liste des transfert pour le site en cours
	$transferts = $transfertTable->getAllBySite($siteId, $typeId);

	return array(
	    'transferts'	 => $transferts,
	    'type'		 => $typeId,
	);
    }

    public function addAction() {
	// table
	$siteTable = $this->getService('Table\SiteTable');

	// Variables
	$siteId = $this->params()->fromRoute('id', 1);

	// Site en cours
	$site = $siteTable->get($siteId);

	// Liste des sites
	$sites = $siteTable->getAll();

	return array(
	    'site'	 => $site,
	    'sites'	 => $sites,
	);
    }

    public function employeListBySiteAction() {
	// Changement de Layout
	$this->layout('layout/ajax');

	// Tables
	$employeTable	 = $this->getService('Table\EmployeTable');
	$siteTable	 = $this->getService('Table\SiteTable');

	// Variables
	$siteId		 = $this->params()->fromRoute('id', 1);
	$siteArriveeId	 = $this->params()->fromRoute('other1', 1);

	// Liste
	$employes = $employeTable->getEmployesBySite($siteId);

	// Site d'arrivée
	$siteArrivee	 = $siteTable->get($siteArriveeId);
	$siteDepart	 = $siteTable->get($siteId);

	return array(
	    'employes'	 => $employes,
	    'siteDepart'	 => $siteDepart,
	    'siteArrivee'	 => $siteArrivee,
	);
    }

    /**
     * Effectuer un transfert
     * 
     * @return array
     */
    public function makeTransfertAction() {
	// Data en Post
	$post = $this->getRequest()
		->getPost();

	// Table
	$employeTable		 = $this->getService('Table\EmployeTable');
	$transfertTable		 = $this->getService('Table\TransfertTable');
	$planningSemaineTable	 = $this->getService('Table\PlanningSemaineTable');

	// Variables
	$matricule	 = $post->get('matricule');
	$arrivee	 = $post->get('arrivee');
	//$day		 = $post->get('jour');
	$entree		 = $post->get('entree');
	$sortie		 = $post->get('sortie');
	$pause		 = $post->get('pause');
	$repas		 = $post->get('repas');

	// L'employé
	$employe	 = $employeTable->get($matricule);
	//$employe->site_id	 = $arrivee;
	// Ligne de transfert
	$transfert	 = new \RHX\Model\Object\Transfert($transfertTable->getAdapter(), $post);
	$transfertTable->save($transfert);

	// Rechercher dans le planning
	var_dump($post->jour);
	var_dump(\RHX\Helper\RHDateTime::dayName($post->jour));
	$planning = $planningSemaineTable->getPlanningByDay($post->jour, $matricule);
	$id = null;
	if ($planning && (int)$planning->id != 0) {
	    $day = \RHX\Helper\RHDateTime::dayName($post->jour);
	    $dayCode = $day.'_code';
	    var_dump($dayCode);
	    $dayEntree = $day.'_entree';
	    $daySortie = $day.'_sortie';
	    $dayPause = $day.'_pause';
	    $dayRepas = $day.'_repas';
	    // Mise à jour
	    $planning->$dayCode = \RHX\Model\Object\XRules::TransfertSortant;
	    $planning->$dayEntree = $entree;
	    $planning->$daySortie = $sortie;
	    $planning->$dayPause = $pause;
	    $planning->$dayRepas = $repas;
	    var_dump($planning);
	    // Enregistrement
	    //$employeTable->save($employe);
	    $id = $planningSemaineTable->save($planning);
	}
//	return $this->redirect()
//			->toRoute('RHX/default', array('controller'	 => 'transfert', 'action'	 => 'index', 'id'		 => $arrivee, 'other1'	 => 1));

	return array(
	    'employe'	 => $employe,
	    'transfert'	 => $transfert,
	    'planning'	 => $planning,
	    'post'		 => $post,
	    'id' => $id,
	);
    }

    /**
     * Afficher les données de transfert
     * 
     * @return array
     */
    public function planningTransfertAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// POST
	$post = $this->getRequest()
		->getPost();

	// Table Site
	$siteTable = new \RHX\Model\Table\SiteTable($this->getAdapter());

	// Liste des sites à afficher
	$siteList = $siteTable->getSomeSites(array($post->site));

	// Site en Cours
	$site = $siteTable->get($post->site);

	return array(
	    'sites'		 => $siteList,
	    'site'		 => $site,
	    'lundi'		 => $post->lundi,
	    'dimanche'	 => $post->dimanche,
	);
    }

    public function getEmployeListBySiteAction() {
	// Changement de Layout
	$this->layout('layout/ajax');

	// POST
	$post = $this->getRequest()
		->getPost();

	// Employe Table
	$employeTable = new \RHX\Model\Table\EmployeTable($this->getAdapter());

	// Liste des employés pour un site
	$employes = $employeTable->getEmployesBySite($post->site);

	return array(
	    'employes' => $employes,
	);
    }

    public function cancelAction() {
	// Table Transfert
	$tableTransfert = new \RHX\Model\Table\TransfertTable($this->getAdapter());

	// POST
	$post = $this->getRequest()
		->getPost();

	// Transfert en cours
	$transfert = $tableTransfert->get($post->id);

	// Recherche du planning semaine du transfert
	$tablePlanning	 = new \RHX\Model\Table\PlanningSemaineTable($this->getAdapter());
	$planning	 = $tablePlanning->getPlanningByDay($transfert->journee, $transfert->matricule);

	// Préfix jour de la date
	$prefix = \RHX\Helper\RHDateTime::dayName($transfert->journee);

	// Variable dynamique
	$prefixCode	 = $prefix . '_code';
	$prefixEntree	 = $prefix . '_entree';
	$prefixSortie	 = $prefix . '_sortie';
	$prefixPause	 = $prefix . '_pause';
	$prefixRepas	 = $prefix . '_repas';

	// Ajout manuelle
	$post->code	 = 3;
	$post->entree	 = '00:00';
	$post->sortie	 = '00:00';
	$post->pause	 = 0;
	$post->repas	 = 0;
	//var_dump($post);
	//var_dump($planning);
	// var_dump($transfert);
	// Changement dans le planning
	if ($planning) {
	    $planning->$prefixCode	 = (int) $post->code;
	    $planning->$prefixEntree = $post->entree;
	    $planning->$prefixSortie = $post->sortie;
	    $planning->$prefixPause	 = (int) $post->pause;
	    $planning->$prefixRepas	 = (int) $post->repas;

	    // Mise à jour du planning
	    $tablePlanning->save($planning);
	}

	// Effacer le transfert en cours
	$nb = $tableTransfert->erase($post->id);

	return new \Zend\View\Model\JsonModel(array(
	    'transfert'	 => $transfert,
	    'prefix'	 => $prefix,
	    'planning'	 => $planning,
	    'nb'		 => $nb,
	));
    }

    /**
     * Editer le transfert depuis le planning semaine
     * 
     * @return \Zend\View\Model\JsonModel
     */
    public function editFromPlanningAction() {
	// changement de layout
	$this->layout('layout/ajax');
	// table Transfert
	$tableTransfert		 = new \RHX\Model\Table\TransfertTable($this->getAdapter());
	// post
	$post			 = $this->getRequest()
		->getPost();
	// transfert en cours
	$transfert		 = $tableTransfert->get($post->id);
	$transfert->entree	 = $post->entree;
	$transfert->sortie	 = $post->sortie;
	$transfert->pause	 = $post->pause;
	$transfert->repas	 = $post->repas;
	$transfert->commentaire	 = $post->commentaire;
	// sauvegarde
	$id			 = $tableTransfert->save($transfert);
	$done			 = false;
	if ($id == (int) $transfert->id) {
	    $done = true;
	}
	// planning semaine : mise à jour
	$this->updatePlanning($transfert);

	return new \Zend\View\Model\JsonModel(array(
	    'done'		 => $done,
	    'transfert'	 => $transfert,
	));
    }
    
    /**
     * Détermine si un transfert est possible ou pas
     * 
     * @return \Zend\View\Model\JsonModel
     */
    public function allowTransfertAction(){
	$this->layout('layout/ajax');
	$post = $this->getRequest()
		->getPost();
	$manager = new \RHX\Model\Manager\TransfertManager($this->getAdapter());
	$data = $manager->isAllowed($post->matricule, $post->date, $post->entree, $post->sortie, $post->id);
	return new \Zend\View\Model\JsonModel(array(
	    'allowed' => $data['allowed'],
	    'message' => $data['message'],
	));
    }
    
    /**
     * Mise à jour du planning semaine où se trouve le transfert sortant
     * 
     * @param \RHX\Model\Object\Transfert $transfert
     * @return boolean
     */
    private function updatePlanning(\RHX\Model\Object\Transfert $transfert){
	$tablePlanning = new \RHX\Model\Table\PlanningSemaineTable($this->getAdapter());
	$planning = $tablePlanning->getPlanningByDay($transfert->journee, $transfert->matricule);
	$day = \RHX\Helper\RHDateTime::dayName($transfert->journee);
	$dayCode = $day.'_code';
	$dayEntree = $day.'_entree';
	$daySortie = $day.'_sortie';
	$dayPause = $day.'_pause';
	$dayRepas = $day.'_repas';
	$planning->$dayCode = \RHX\Model\Object\XRules::TransfertSortant;
	$planning->$dayEntree= $transfert->entree;
	$planning->$daySortie = $transfert->sortie;
	$planning->$dayPause = $transfert->pause;
	$planning->$dayRepas = $transfert->repas;
	$id = 0;
	$id = $tablePlanning->save($planning);
	return ($id != 0) ? true : false;
    }

}

?>
